package com.sykj.seaflow.sys.api;


import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import com.sykj.seaflow.sys.modual.dept.entity.SysDept;
import com.sykj.seaflow.sys.modual.dept.service.SysDeptService;
import com.sykj.seaflow.sys.modual.position.entity.SysPosition;
import com.sykj.seaflow.sys.modual.position.service.SysPositionService;
import com.sykj.seaflow.sys.modual.positionuser.service.PositionUserService;
import com.sykj.seaflow.sys.modual.role.entity.SysRole;
import com.sykj.seaflow.sys.modual.role.service.SysRoleService;
import com.sykj.seaflow.sys.remote.api.SysPermissionApi;
import com.sykj.seaflow.sys.remote.bean.LoginUserInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@AllArgsConstructor
@Component
public class SysPermissionApiProvider implements SysPermissionApi {
    private final SysDeptService sysDeptService;

    private final SysPositionService positionService;

    private final SysRoleService roleService;


    @Override
    public void loadPermission() {
        LoginUserInfo loginUserInfo = new LoginUserInfo();
        // 获取角色
        List<SysRole> sysRoles = roleService.myRoles();
        loginUserInfo.setRoleList(CollStreamUtil.toList(sysRoles,SysRole::getRoleCode));
        // 职位
        List<SysPosition> sysPositions =  positionService.myPosition();

        loginUserInfo.setPosList(CollStreamUtil.toList(sysPositions, SysPosition::getId));
        // 职位不为空
        if(ObjectUtil.isNotEmpty(sysPositions)){
            List<Long> list = CollStreamUtil.toList(sysPositions, SysPosition::getDeptId).stream().distinct().collect(Collectors.toList());//.toList();
            // 部门
            List<SysDept> sysDepts = sysDeptService.listByIds(list);
            loginUserInfo.setDeptList(CollStreamUtil.toList(sysDepts,SysDept::getId));
        }
    }

}
